import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:my_flutter/http/api/ApiGet.dart';
import 'package:my_flutter/model/LoginInfo.dart';
import 'package:my_flutter/store/Book.dart';
import 'package:my_flutter/store/LoginData.dart';
import 'package:my_flutter/store/Person.dart';
import 'package:my_flutter/core/Routers.dart';
import 'package:my_flutter/store/Store.dart';
import 'package:provider/provider.dart';

///@ClassName: MyHomePage
/// @Author: guoyangyang
///@Date: 2022 3/9/22 2:15 PM
///@Description:

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;

  Map<String, int>? map;

  var json = Store.of<LoginStore>().mData?.toJson().toString();

  void userInfo() async{
    //登录
    LoginInfo loginInfo = await ApiGet.loginInfo();
    if (loginInfo.dioError != null) {
      debugPrint(loginInfo.dioError?.errorMsg);
      return;
    }
    debugPrint(loginInfo.toJson().toString());
  }

  @override
  void initState() {
    super.initState();
    //    var result = Routers.push(RouteName.loginPage);
//    result.then((value) => {
//          if (value != null)
//            {
//              map = (value as Map<String, int>),
//              debugPrint(map?.length.toString()),
//            }
//        });
  }

  @override
  Widget build(BuildContext context) {
    debugPrint('HomePage------------build');

    Provider.of<Book>(context, listen: false).bookName = "《诗》《书》《礼》《乐》";

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("MyHomePage"),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
                json ??= "未登陆"
            ),
            Text(
              Provider.of<Book>(context).bookName
            ),
            const SizedBox(
              height: 50,
            ),

          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: userInfo,
        tooltip: '获取用户信息',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
